package com.daihou.BCJ.decrease;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.daihou.BCJ.repayment.BalanceRepayment;
import com.daihou.config.TheAuth;
import com.daihou.model.ceres.DecreaseUrlEnum;
import com.daihou.utils.DHConfigFile;
import com.daihou.utils.DHHttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.*;

/**
 * @author liujintao
 * @date 2021/2/8 10:37
 */
public class DecreasespecialConfig {
    private static final Logger logger = LoggerFactory.getLogger(BalanceRepayment.class);
    private String url;
    Map<String, String> headers = new HashMap<>();
    private int id;
    private int classifyId;

    @BeforeTest(description = "测试准备工作,获取Authorization对象")
    public void beforeTest() throws IOException {
        url = DHConfigFile.getUrl("DaiHou_URL", "decrease", DecreaseUrlEnum.DECREASESPECIALCONFIGADD.toString());
        //设置请求头中的Authorization
        headers.put("Authorization", TheAuth.getAuth());
        headers.put("X-UserName", "admin");
    }

    @Test(description = "特殊减免配置新增")
    public void decreaseSpecialConfigAddTest() {
        String body = "{\"name\":\"test\",\"classifyId\":16}";
        String result = Objects.requireNonNull(DHHttpUtil.post_with_String(url, body, headers)).toJSONString();
        logger.info("特殊减免配置新增结果：{}", result);
        Assert.assertTrue(result.contains("\"message\":\"配置名称重复，请检查！\",\"status\":\"-1\""));
    }

    @Test(description = "特殊减免配置查询", dependsOnMethods = "decreaseSpecialConfigAddTest")
    public void decreaseSpecialConfigListQueryTest() {
        url = DHConfigFile.getUrl("DaiHou_URL", "decrease", DecreaseUrlEnum.DECREASESPECIALCONFIGLISTQUERY.toString());
        String body = "{\"pageNum\":1,\"pageSize\":20,\"order\":null,\"name\":\"\",\"fundItemCode\":\"\",\"valid\":\"\"}";
        String result = Objects.requireNonNull(DHHttpUtil.post_with_String(url, body, headers)).toJSONString();
        logger.info("特殊减免配置查询结果：{}", result);
        Assert.assertTrue(result.contains("\"message\":\"操作成功\",\"status\":\"0\""));
        JSONArray list = JSON.parseObject(result).getJSONObject("data").getJSONArray("list");
        id = list.getJSONObject(0).getIntValue("id");
        classifyId = list.getJSONObject(0).getIntValue("classifyId");
        logger.info("id值：{}", id);

    }

    @Test(description = "特殊减免配置更新", dependsOnMethods = "decreaseSpecialConfigListQueryTest")
    public void decreaseSpecialConfigUpdateTest() {
        url = DHConfigFile.getUrl("DaiHou_URL", "decrease", DecreaseUrlEnum.DECREASESPECIALCONFIGUPDATE.toString());
        String body = "{\"name\":\"特殊减免配置666\",\"classifyId\":" + classifyId + ",\"id\":" + id + "}";
        String result = Objects.requireNonNull(DHHttpUtil.post_with_String(url, body, headers)).toJSONString();
        logger.info("特殊减免配置更新结果：{}", result);
        Assert.assertTrue(result.contains("\"message\":\"操作成功\",\"status\":\"0\""));
    }

    @Test(description = "特殊减免配置状态修改", dependsOnMethods = "decreaseSpecialConfigUpdateTest")
    public void decreaseSpecialConfigStatusUpdateTest() {
        url = DHConfigFile.getUrl("DaiHou_URL", "decrease", DecreaseUrlEnum.SPECIALCONFIGUPDATESTATUS.toString());
        String body = "{\"id\":"+id+",\"valid\":1}";
        String result = Objects.requireNonNull(DHHttpUtil.post_with_String(url, body, headers)).toJSONString();
        logger.info("特殊减免配置状态修改结果：{}", result);
        Assert.assertTrue(result.contains("\"message\":\"操作成功\",\"status\":\"0\""));
    }

    @Test(description = "特殊减免配置日志查询", dependsOnMethods = "decreaseSpecialConfigStatusUpdateTest")
    public void configLogQueryTest() {
        url = DHConfigFile.getUrl("DaiHou_URL", "decrease", DecreaseUrlEnum.SPECIALCONFIGLOGQUERY.toString());
        String body = "{\"pageNum\":1,\"pageSize\":20,\"order\":null,\"id\":"+id+"}";
        String result = Objects.requireNonNull(DHHttpUtil.post_with_String(url, body, headers)).toJSONString();
        logger.info("特殊减免配置日志查询结果：{}", result);
        Assert.assertTrue(result.contains("\"message\":\"操作成功\",\"status\":\"0\""));
    }
}